﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using dotUML.Core.Common;
using dotUML.Core.Parser;
using dotUML.System;

namespace dotUML.Core
{
    /// <summary>
    /// DiagramEngine class implements the IDiagramEngine interface. This class
    /// is only accessible within dotUML.Core namespace. It provides top-level 
    /// functionality of the dotUML toolkit.
    /// </summary>
    class DiagramEngine : IDiagramEngine
    {
        IXmlParser _parser;

        /// <summary>
        /// XmlParser object.
        /// </summary>
        public IXmlParser Parser
        {
            get { return _parser; }
            set { _parser = value; }
        }

        /// <summary>
        /// Class constructor.
        /// </summary>
        public DiagramEngine()
        {
            Errors = new List<IError>();
            Parser = XmlParserFactory.GetParser();
        }

        #region IDiagramEngine Members

        List<IError> _errors;

        /// <summary>
        /// Errors which occurred while validating or processing xml.
        /// </summary>
        public List<IError> Errors
        {
            get { return _errors; }
            set { _errors = value; }
        }

        /// <summary>
        /// Checks if a given string is a valid dotUML document
        /// </summary>
        /// <param name="dotUMLDocument">a string containing a dotUML document</param>
        /// <returns>true, if validation was successful. false, otherwise</returns>
        public bool Validate(string dotUMLDocument)
        {
            try
            {
                if (!Parser.ValidateXml(dotUMLDocument))
                {
                    Errors.AddRange(Parser.Errors);
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ErrorFactory.NewError(EErrorType.Error, ex.Message));
                // TODO: log exception
            }

            return (Errors.Count == 0);
        }

        /// <summary>
        /// Renders the diagram given in the dotUML document
        /// </summary>
        /// <param name="dotUMLDocument">a string containing a dotUML document</param>
        /// <returns>List of .jpg files in the form of byte arrays</returns>
        public List<byte[]> Process(string dotUMLDocument)
        {
            try
            {
                if (!Parser.Parse(dotUMLDocument, GetModel()))
                {
                    Errors.AddRange(Parser.Errors);
                }
            }
            catch (Exception ex)
            {
                Errors.Add(ErrorFactory.NewError(EErrorType.Error, ex.Message));
                // TODO: log exception
            }

            throw new NotImplementedException();
        }


        /// <summary>
        /// Creates an instance of a dotUML model to represent a dotUML document in memory
        /// </summary>
        /// <returns>dotUML model via IModel interface</returns>
        public dotUML.Core.Common.IModel GetModel()
        {
            return ModelFactory.GetModel();
        }

        #endregion
    }
}
